#include<EnergysEventsList.h>
#include "DateList.h"
#include "Event.h"
#include <iostream.h>


EnergysEventsList::EnergysEventsList(string name_)
{
	this->name = name_;
	energysTyps = vector<DateList*>();


	DateList* Oil = new DateList("oil");
	DateList* Gas = new DateList("gas");
	DateList* Coal = new DateList("coal");
	DateList* Sun = new DateList("sun");
	DateList* Wind = new DateList("wind");
	DateList* Smile = new DateList("smile");
	DateList* Hug = new DateList("hug");
	energysTyps.push_back(Oil);
	energysTyps.push_back(Gas);
	energysTyps.push_back(Coal);
	energysTyps.push_back(Sun);
	energysTyps.push_back(Wind);
	energysTyps.push_back(Smile);
	energysTyps.push_back(Hug);
}

EnergysEventsList::~EnergysEventsList()
{
	this ->clear();
}

void EnergysEventsList::clear()
{
	vector<DateList*>::iterator it;
	for(it = energysTyps.begin();it< energysTyps.end();)
	{
		DateList* date = *it;
	//	date ->clear();
		delete (date);
		date = 0 ;
		energysTyps.erase(it);
	}
}


void EnergysEventsList::addEvent(Event* e)
{
	vector<DateList*>::iterator it;
	bool added = false;
	for(it = energysTyps.begin();it<energysTyps.end() & !added; it++)
	{
		DateList* j = *it;
//		j->print();//dell
		if(e->getEnergyType()==j->getName())
		{
			j->addEvent(e);
			added = true;
		}
	}
	if (!added)
	{
		delete (e);
	}
}

bool EnergysEventsList::noMoreEvents()
{
	bool ans = true;
	vector<DateList*>::iterator it;
	for(it = energysTyps.begin();it<energysTyps.end()&ans; it++)
	{
		DateList* j = *it;
		if(!j->noMoreEvents())
		{
			ans = false;
		}
	}
	return ans;
}

string EnergysEventsList::getName()
{
	return name;
}

Event EnergysEventsList::getFirst(string name, DateTime date)
{
	vector<DateList*>::iterator it;
	for(it = energysTyps.begin();it<energysTyps.end(); it++)
	{
		DateList* j = *it;
		if(name == j->getName())
		{
			return j->getFirst(date);
		}
	}
	return Event();
}
DateTime EnergysEventsList::getDay()
{
	DateTime last = DateTime(9999,12,31);
//	DateTime ans=events.at(0)->getFirstDate();
	vector<DateList*>::iterator it;
		for(it = energysTyps.begin();it<energysTyps.end(); it++)
		{
			DateList* j = *it;
			if(j->getFirstDate()<last)
				last = j->getFirstDate();
		}
		return last;
}

void EnergysEventsList::print()
{
	vector<DateList*>::iterator it;
	for(it = energysTyps.begin();it<energysTyps.end(); it++)
	{
		DateList* j = *it;
		j -> print();
	}
}
